package com.starry.push.netty.listen;


import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.starry.push.netty.config.RabbitConfig;
import com.starry.push.netty.service.PushService;
import com.starry.push.netty.vo.PushInfoData;
import com.starry.push.netty.vo.OrderDetail;
import com.starry.push.netty.vo.ResponseResult;
import com.starry.push.netty.vo.SendAppMsg;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

/**
 * @author WuChao
 * @version 1.0
 * @date 2020/11/9 15:20
 */
@Slf4j
@Component
@AllArgsConstructor
public class PushListener {


    private RabbitTemplate rabbitTemplate;
    private PushService pushService;

    @RabbitHandler
    @RabbitListener(queues = "MessageQueue")
    @SendTo()
    public String commonPush(String message, Channel channel,@Header(AmqpHeaders.DELIVERY_TAG) long tag) {

      ResponseResult result=  ResponseResult.error();
        try {
            PushInfoData data = JSONObject.parseObject(message,PushInfoData.class);
            String pushMsg = data.getMsg();
            Object o = JSONObject.parseObject(pushMsg, Object.class);
            String uniqueIdentification = data.getUniqueIdentification();
            if (StringUtils.isNotBlank(uniqueIdentification)) {
                HttpEntity  httpEntity = pushService.commonPush(null, uniqueIdentification,o );
                log.info("本次推送结果：{}", JSONObject.toJSONString(httpEntity));
                rabbitTemplate.convertAndSend(RabbitConfig.ITEM_TOPIC_EXCHANGE, RabbitConfig.MESSAGE_RESULT_KEY, JSONObject.toJSONString(httpEntity));
                result = (ResponseResult) httpEntity.getBody();
                return JSONObject.toJSONString(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("本次推送失败");
            return  JSONObject.toJSONString(ResponseResult.error());
        } finally {
            try {
                // 返回确认状态
                channel.basicAck(tag, false);
            } catch (Exception e) {
                log.error("RabbitMQ 网关日志ACK异常", e);
            }
        }
        return JSONObject.toJSONString(result);
    }

@RabbitListener(queues = "MessageResultQueue")
    public void pushResult(String message, Channel channel,@Header(AmqpHeaders.DELIVERY_TAG) long tag) {

        try  {
            HttpEntity httpResVo = JSONObject.parseObject(message, HttpEntity.class);
            log.info("接收到一次MQ异步推送结果：{}", JSONObject.toJSONString(httpResVo));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            try {
                // 返回确认状态
                channel.basicAck(tag, false);
            } catch (Exception e) {
                log.error("RabbitMQ 网关日志ACK异常", e);
            }
        }
    }




}





